package com.chat.datea.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.AlbumInfo_Recyc_Adapter;
import com.chat.datea.base.BaseFragment;
import com.chat.datea.base.Finnal_Base;
import com.chat.datea.bean.AlbumInfo;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;


/**
 * Created by xyb on 15/10/26.
 */
public class AlbumInfoFragment extends BaseFragment implements View.OnClickListener {
    private TextView music_num, tag;
    private Toolbar toolbar;
    private RecyclerView recyclerView;
    private String url;
    private VolleySingleton singleton;
    private LinearLayout ly;
    private ImageView singer_img;

    public AlbumInfoFragment(String album_id) {
        url = Finnal_Base.apiUrl + Finnal_Base.method + "baidu.ting.album.getAlbumInfo" + Finnal_Base.format + Finnal_Base.album_id + album_id;
        Log.e("AlbumInfoFragment", url);
        singleton = VolleySingleton.getVolleySingleton(mActivity);
    }

    @Override
    protected void initData() {
        ly.setVisibility(View.GONE);
        toolbar.setNavigationIcon(R.mipmap.nav_back);
        toolbar.setNavigationOnClickListener(this);
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                AlbumInfo albumInfo = gson.fromJson(response, AlbumInfo.class);
                if (albumInfo.getSonglist() != null && albumInfo.getSonglist().size() > 0) {
                    music_num.setText("共" + albumInfo.getSonglist().size() + "首歌");
                    if (albumInfo.getAlbumInfo().getPic_s500() != null) {
                        Picasso.with(mActivity).load(albumInfo.getAlbumInfo().getPic_s500()).placeholder(R.mipmap.default_ticker).into(singer_img);
                    }
                    AlbumInfo_Recyc_Adapter recycAdapter = new AlbumInfo_Recyc_Adapter(mActivity, albumInfo);
                    recyclerView.setAdapter(recycAdapter);
                    recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        singleton.addQueue(request, "AlbumInfoFragment");
    }

    @Override
    protected void initView() {
        music_num = (TextView) view.findViewById(R.id.music_num);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        tag = (TextView) view.findViewById(R.id.tag);
        ly = (LinearLayout) view.findViewById(R.id.ly);
        singer_img = (ImageView) view.findViewById(R.id.singer_img);
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_todayday, null);
        return view;
    }

    @Override
    public void onClick(View v) {
        getActivity().getSupportFragmentManager().popBackStack();
    }
}
